<script setup lang="ts">
import {addCoachAPI, deleteCoachAPI, searchAPI} from "@/api/destribute.ts";
import {reactive, ref} from "vue";
import {ElMessage, ElNotification} from "element-plus";
import {formattedDate} from "@/utils/formatDate.ts";
import {Delete, Edit, InfoFilled} from "@element-plus/icons-vue";
import {useCoachStore} from "@/store/coach.ts";

// 获取教练列表
const coachStore = useCoachStore()

// 添加教练
const dialogAddCoachVisible = ref(false)
const value1 = ref()
const coachData = reactive({
  name: '',
  age: '',
  avatar: '',
  address: '',
  schedule: {
    start: '',
    end: ''
  },
  phone: '',
  salary: '',
  desc: '',
  scope: ''
})

// 上传头像
const handlerAvatarSuccess = async (res) => {
  coachData.avatar = res.data
}
const addCoach = async () => {
  coachData.schedule.start = value1.value[0]
  coachData.schedule.end = value1.value[1]
  const res = await addCoachAPI(coachData)
  if (res.code === 0) {
    dialogAddCoachVisible.value = false
    await coachStore.getCoachList()
    ElMessage({
      type: 'success',
      message: res.message
    })
  }
}

// 删除教练
const deleteCoach = async (id) => {
  const res = await deleteCoachAPI(id)
  if (res.code === 0) {
    await coachStore.getCoachList()
    ElMessage({
      type: 'success',
      message: res.message
    })
  }
}

// 查询教练
const coachName = ref('')
const dialogCoachDetailVisible = ref(false)
const searchData = ref()
const searchCoach = async () => {
  dialogCoachDetailVisible.value = true
  const res = await searchAPI(coachName.value)
  searchData.value = res.data
}

// 编辑教练
const EditCoach = () => {
  ElNotification({
    title: 'Warning',
    message: '为减缓服务器压力，暂关闭编辑接口及数据回显',
    type: 'warning',
  })
}
</script>

<template>
  <el-card class="box-card">
    <div class="card-coach-title">
      <div class="search-coach align_center row">
        <el-input v-model="coachName" placeholder="请输入教练名字" style="margin-right: 20px"/>
        <el-button type="primary" @click="searchCoach">查询教练</el-button>
      </div>
      <el-button type="primary" @click="dialogAddCoachVisible = true">添加教练</el-button>
    </div>
    <div class="card-coach-table">
      <el-table
          border
          class="el-table-box"
          ref="multipleTableRef"
          :data="coachStore.coachList"
          style="width: 100%"
      >
        <el-table-column label="#" width="60" align="center" type="index"/>
        <el-table-column prop="name" align="center" label="名称" width="80"/>
        <el-table-column prop="age" align="center" label="年龄" width="60"/>
        <el-table-column prop="phone" align="center" label="手机号" width="120"/>
        <el-table-column prop="scope" align="center" label="评分" width="60"/>
        <el-table-column prop="salary" align="center" label="薪资" width="80"/>
        <el-table-column prop="address" align="center" label="地址" width="120"/>
        <el-table-column align="center" label="描述" width="160">
          <template #default="{row}">
            <p class="limit-row">
              {{ row.desc }}
            </p>
          </template>
        </el-table-column>
        <el-table-column align="center" label="入职时间" width="120">
          <template #default="{row}">
            {{ formattedDate(row.createdAt) }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="控制">
          <template #default="{row}">
            <div class="operator-btn">
              <el-button type="primary" size="default" :icon="Edit as any" @click="EditCoach" circle/>
              <el-popconfirm
                  width="200"
                  @confirm="deleteCoach(row._id)"
                  confirm-button-text="确定"
                  cancel-button-text="取消"
                  :icon="InfoFilled as any"
                  icon-color="#f36b6c"
                  title="您确定删除此设备吗?"
              >
                <template #reference>
                  <el-button type="danger" size="default" :icon="Delete as any" circle/>
                </template>
              </el-popconfirm>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </el-card>

  <el-dialog :close-on-click-modal="false" v-model="dialogCoachDetailVisible" title="教练详情">
    <div class="dialog-item">
      <span>名称：</span>
      <p>{{ searchData?.name }}</p>
    </div>
    <div class="dialog-item">
      <span>年龄：</span>
      <p>{{ searchData?.age }}</p>
    </div>
    <div class="dialog-item">
      <span>手机号：</span>
      <p>{{ searchData?.phone }}</p>
    </div>
    <div class="dialog-item">
      <span>评分：</span>
      <p>{{ searchData?.scope }}</p>
    </div>
    <div class="dialog-item">
      <span>地址：</span>
      <p>{{ searchData?.address }}</p>
    </div>
  </el-dialog>
  <el-dialog :close-on-click-modal="false" v-model="dialogAddCoachVisible" title="添加教练">
    <el-form
        :model="coachData"
        status-icon
        class="demo-ruleForm row"
    >
      <div>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="coachData.name" autocomplete="off"/>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model.number="coachData.age" autocomplete="off"/>
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="coachData.phone" autocomplete="off"/>
        </el-form-item>
        <el-form-item label="薪资" prop="salary">
          <el-input v-model.number="coachData.salary" autocomplete="off"/>
        </el-form-item>
        <el-form-item label="评分" prop="scope">
          <el-input v-model.number="coachData.scope" autocomplete="off"/>
        </el-form-item>
        <el-form-item label="上传头像">
          <el-upload
              ref="uploadRef"
              class="upload-demo"
              :on-success="handlerAvatarSuccess"
              action="http://116.62.147.119:3636/admin/coach/avatar"
          >
            <el-button type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>
      </div>
      <div style="margin-left: 20px" class="date-select-div">
        <el-form-item label="地址" prop="address">
          <el-input v-model="coachData.address" autocomplete="off"/>
        </el-form-item>
        <el-form-item label="排班" prop="schedule">
          <el-date-picker
              v-model="value1"
              type="datetimerange"
              start-placeholder="起始"
              end-placeholder="结束"
          />
        </el-form-item>
        <el-form-item label="描述" prop="desc">
          <el-input
              v-model="coachData.desc"
              resize="none"
              type="textarea"
              :rows="4"
              placeholder="来一段自我描述吧..."
          />
        </el-form-item>
        <el-button class="confirm-btn" type="primary" @click="addCoach">确定</el-button>
      </div>
    </el-form>
  </el-dialog>
</template>

<style scoped lang="scss">
.box-card {
  padding: 0 50px;

  .card-coach-title {
    height: 35px;
    display: flex;
    justify-content: space-between;
  }

  .card-coach-table {
    margin-top: 20px;

    .el-table-box {
      height: calc(90vh - 140px);

      .el-table__cell {
        position: static !important; // 解决el-image 和 el-table冲突层级冲突问题
      }
    }

    .coach-desc {
      display: -webkit-box; /*弹性伸缩盒子*/
      -webkit-box-orient: vertical; /*垂直排列*/
      line-height: 20px;
      -webkit-line-clamp: 1;
      overflow: hidden;
      text-overflow: ellipsis;
    }
  }
}

.date-select-div {
  position: relative;

  .confirm-btn {
    position: absolute;
    bottom: 10px;
    right: 0;
    width: 100px;
  }
}

.limit-row {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
  -webkit-line-clamp: 2;
}

.dialog-item {
  display: flex;
  flex-direction: row;
  height: 30px;
  align-items: center;

  p {
    margin-left: 12px;
  }
}
</style>